package run.siyuan.poi.policy;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.deepoove.poi.data.CellRenderData;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.Rows;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.policy.DynamicTableRenderPolicy;
import com.deepoove.poi.policy.TableRenderPolicy;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import run.siyuan.poi.model.AttributeEntity;

import java.util.*;

/**
 * @ClassName MyDynamicPolicy
 * @Description TODO
 * @Author siyuan
 * @Date 2023/6/10 11:03
 */
public class MyDynamicPolicy extends DynamicTableRenderPolicy {
    @Override
    public void render(XWPFTable table, Object data) throws Exception {
        if (null == data) {
            return;
        }

        List<LinkedHashMap<String, String>> labors = (List<LinkedHashMap<String, String>>) data;
        if (null != labors) {
            // 循环插入行
            for (int i = 0; i < labors.size(); i++) {
                //
                XWPFTableRow insertNewTableRow = table.insertNewTableRow(1);
                LinkedHashMap<String, String> linkedHashMap = labors.get(i);
                Collection<String> values = linkedHashMap.values();

                //处理一行有几列
                for (int j = 0; j < values.size(); j++) {
                    insertNewTableRow.createCell();
                }
                String[] array = ArrayUtil.toArray(values, String.class);
                RowRenderData rowRenderData1 = Rows.create(array);
                // 单行数据渲染
                TableRenderPolicy.Helper.renderRow(insertNewTableRow, rowRenderData1);
            }
        }
    }
}
